package com.kma.summer2012.client.presenters.mainPage.widgets;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionModel;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.constants.ConstantsHolder;
import com.kma.summer2012.client.events.FillCellTableEvent;
import com.kma.summer2012.client.events.SortByDateEvent;
import com.kma.summer2012.client.events.UpdateCellTableEvent;
import com.kma.summer2012.client.events.engineer.EngineerShowRequestEvent;
import com.kma.summer2012.client.events.manager.EditPageEventManager;
import com.kma.summer2012.client.events.manager.ShowRequestManager;
import com.kma.summer2012.client.events.operator.EditPageEvent;
import com.kma.summer2012.client.events.request.RequestsSelectedEvent;
import com.kma.summer2012.client.view.mainPage.ColumnsFactory;
import com.kma.summer2012.shared.model.UICrumbsRequest;
import com.kma.summer2012.shared.model.UICrumbsUser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: EUGENE
 * Date: 3/2/13
 * Time: 1:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class MainTablePresenter extends PositionHolder {
    public interface Display {
        ListDataProvider<UICrumbsRequest> fillDataGrid(List<UICrumbsRequest> list);

        void setSelectionModelForTable(SelectionModel<UICrumbsRequest> selectionModel);

        void setDoubleClickHandler(DoubleClickHandler doubleClickHandler);

        void configureDataGrid(List<Column> columns, ColumnSortEvent.ListHandler<UICrumbsRequest> sortHandler);

        HasClickHandlers getUpdateButton();

        void cleanCellTable();

        Integer getTableID();

        void sortDateColumn(String date);

        void setAdditionalControls(Widget widget);

        void setPosition(String position);
    }


    private Display display;
    private CrumbsEventBus eventBus = CrumbsEventBus.getInstance();

    public Boolean showEdit = true;

    public MainTablePresenter(Display display) {
        super();
        this.display = display;
        this.display.setPosition(this.position);
        this.bind();
    }


    private void bind() {
        this.display.getUpdateButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                eventBus.fireEvent(new UpdateCellTableEvent(display.getTableID()));
            }
        });
        setSelectionModelForDisplay();

        this.eventBus.addHandler(FillCellTableEvent.getType(), new FillCellTableEvent.FillCellTableEventHandler() {
            @Override
            public void onFillCellTable(FillCellTableEvent event) {
                if ((event.tableID).equals(display.getTableID()))
                    MainTablePresenter.this.fillDataGrid(event.requests);
            }
        });

        this.eventBus.addHandler(SortByDateEvent.getType(), new SortByDateEvent.SortByDateEventHandler() {
            @Override
            public void onSortByDate(SortByDateEvent event) {
                if (event.tableID.equals(display.getTableID())) {
                    doSortByDate(event.date);
                }
            }
        });
    }

    private void doSortByDate(Date date) {
        String dateStr = String.valueOf(DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_SHORT()).format(date));
        display.sortDateColumn(dateStr);
    }

    private void setSelectionModelForDisplay() {
        final MultiSelectionModel<UICrumbsRequest> selectionModel = new MultiSelectionModel<UICrumbsRequest>();
        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
            @Override
            public void onSelectionChange(SelectionChangeEvent selectionChangeEvent) {
                Set<UICrumbsRequest> set = ((MultiSelectionModel) selectionChangeEvent.getSource()).getSelectedSet();
                List<UICrumbsRequest> list = new ArrayList<UICrumbsRequest>();
                list.addAll(set);
                MainTablePresenter.this.eventBus.fireEvent(new RequestsSelectedEvent(list, display.getTableID()));
            }
        });
        this.display.setSelectionModelForTable(selectionModel);

        DoubleClickHandler doubleClickHandler = new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                Set<UICrumbsRequest> set = (selectionModel).getSelectedSet();
                if (set.size() > 1) {
                    return;
                } else {
                    if (set.size() == 1) {
                        UICrumbsRequest request = (UICrumbsRequest) (set.toArray())[0];
                        GwtEvent event = null;
                        if (position.equals(UICrumbsUser.MANAGER_POSITION)) {
                            if (showEdit) {
                                event = (new EditPageEventManager(request));
                            } else {
                                event = (new ShowRequestManager(request));
                            }
                        }
                        if (position.equals(UICrumbsUser.OPERATOR_POSITION)) {
                            event = (new EditPageEvent(request));
                        }
                        if (position.equals(UICrumbsUser.ENGINEER_POSITION)) {
                            event = (new EngineerShowRequestEvent(request));
                        }
                        if (event != null)
                            MainTablePresenter.this.eventBus.fireEvent(event);
                    }
                }
            }
        };
        this.display.setDoubleClickHandler(doubleClickHandler);
    }

    public void fillDataGrid(List<UICrumbsRequest> uiCrumbsRequests) {
        this.display.cleanCellTable();
        ListDataProvider<UICrumbsRequest> ldpr = display.fillDataGrid(uiCrumbsRequests);
        ColumnSortEvent.ListHandler<UICrumbsRequest> sortHandler = new ColumnSortEvent.ListHandler<UICrumbsRequest>(ldpr.getList());
        List<Column> columns = ColumnsFactory.getInstance().getColumnsByPosition(this.position, sortHandler);
        this.display.configureDataGrid(columns, sortHandler);
    }
}
